package com.mathdroid.gui.main.talents;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.mathdroid.core.DroidStringWorker;
import com.mathdroid.gui.ImageLoader;
import com.mathdroid.model.talents.Talent;

/**
 * This class acts like a button and has a mouse listener. It paints the
 * talent icon, then a border above the icon. The points spent in this talent
 * and the maximum points are shown too.
 */
public class TalentButton extends JPanel {
	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Reference to the model of the represented talent.
	 */
	private Talent talent = null;
	
	/**
	 * The border image.
	 */
	private static final Image BORDER = new ImageIcon(
			TalentButton.class.getClass().getResource(
					"/com/mathdroid/gui/images/talent.png")).getImage();
	
	/**
	 * The icon of the talent.
	 */
	private Image talentImg = null;
	
	/**
	 * The font used to show the talent points.
	 */
	private static final Font FONT = new Font("Arial", Font.BOLD, 12);
	
	/**
	 * The color of the spent points.
	 */
	private static final Color LIGHT_BLUE = new Color(157, 237, 255);
	
	/**
	 * The color of the maximum points.
	 */
	private static final Color DARK_BLUE = new Color(0, 62, 104);
	
	/**
	 * String utility.
	 */
	private final DroidStringWorker stringHelper = new DroidStringWorker();
	
	/**
	 * Image loader.
	 */
	private final ImageLoader il = new ImageLoader();
	
	/**
	 * Constructor.
	 * @param t The model of the talent.
	 */
	public TalentButton(final Talent t) {
		this.talent = t;
		
		if (this.talent == null) {
			this.setVisible(false);
		} else {
			this.setIcon();
			this.setToolTip();
		}
		
		this.addMouseListener(new MouseListener() {
			@Override public void mouseClicked(final MouseEvent e) {
				
				if (e.getButton() == MouseEvent.BUTTON1) {
					talent.increase();
					setToolTip();
				}
				
				if (e.getButton() == MouseEvent.BUTTON3) { 
					talent.decrease();
					setToolTip();
				}
				
				repaint();
			}
			@Override public void mouseEntered(final MouseEvent arg0) { }
			@Override public void mouseExited(final MouseEvent arg0) { }
			@Override public void mousePressed(final MouseEvent arg0) { }
			@Override public void mouseReleased(final MouseEvent arg0) { }
		});
	}
	
	/**
	 * Build the tooltip text.
	 */
	private void setToolTip() {
		if (this.talent.getPoints() == 0) {
			this.setToolTipText("<html><b>" + this.talent.getName() + "</b><br>"
					+ this.stringHelper.insertBreaks(
					this.talent.getDescriptions().get(0)) + "</html>");
		} else {
			if (this.talent.getPoints() == this.talent.getMaxPoints()) {
				this.setToolTipText("<html><b>" 
						+ this.talent.getName() + "</b><br><br>"
						+ "<b>Current Rank (" 
						+ this.talent.getPoints() + "):</b><br>" 
						+ this.stringHelper.insertBreaks(
								this.talent.getDescriptions().get(
								this.talent.getPoints() - 1)) + "</html>");
			} else {
				this.setToolTipText("<html><b>" 
						+ this.talent.getName() + "</b><br><br>" 
						+ "<b>Current Rank (" 
						+ this.talent.getPoints() + "):</b><br>" 
						+ this.stringHelper.insertBreaks(
								this.talent.getDescriptions().get(
								this.talent.getPoints() - 1))
						+ "<br><br><b>Next Rank (" 
						+ (this.talent.getPoints() + 1) + "):</b><br>" 
						+ this.stringHelper.insertBreaks(
								this.talent.getDescriptions().get(
								this.talent.getPoints())) + "</html>");
			}
		}
	}

	/**
	 * Set the icon for the observed talent model.
	 */
	private void setIcon() {
		this.talentImg = this.il.getTalentIcon(this.talent.getImage());
	}
	
	/**
	 * Set the represented talent model.
	 * @param t The talent model.
	 */
	public final void setTalent(final Talent t) {
		this.talent = t;
		this.setIcon();
		this.setToolTip();
		this.setVisible(true);
	}
	
	@Override
	public final void paint(final Graphics g) {
		final int talentTopLeftX = 5;
		final int talentTopLeftY = 5;
		final int talentWidth = 40;
		final int talentHeight = 40;
		
		final int borderTopLeftX = 0;
		final int borderTopLeftY = 0;
		final int borderWidth = 50;
		final int borderHeight = 50;
		
		final int talentPointsX = 15;
		final int talentPointsY = 45;
		
		final int talentMaxPointsX = 29;
		final int talentMaxPointsY = 45;
		
		g.drawImage(this.talentImg, talentTopLeftX, talentTopLeftY, 
				talentWidth, talentHeight, null);
		
		g.drawImage(TalentButton.BORDER, borderTopLeftX, borderTopLeftY, 
				borderWidth, borderHeight, null);
		
		g.setFont(TalentButton.FONT);
		
		g.setColor(TalentButton.LIGHT_BLUE);
		g.drawString("" + this.talent.getPoints(), 
				talentPointsX, talentPointsY);
		
		g.setColor(TalentButton.DARK_BLUE);
		g.drawString("" + this.talent.getMaxPoints(), 
				talentMaxPointsX, talentMaxPointsY);
	}
}
